﻿using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Extensions.DependencyInjection;
using RotovapPlatform.UI.ViewModels.DeviceMaintenance.ManualOperation;

namespace RotovapPlatform.UI.Views.DeviceMaintenance.ManualOperation
{
	/// <summary>
	/// SerialDevicesListView.xaml 的交互逻辑
	/// </summary>
	public partial class SerialDevicesListView : UserControl
	{
		private readonly SerialDevicesListViewModel _viewModel;

		public SerialDevicesListView()
		{
			InitializeComponent();

			// 从DI容器获取ViewModel
			_viewModel = App.ServiceProvider.GetRequiredService<SerialDevicesListViewModel>();
			DataContext = _viewModel;

			// 注册Loaded事件，在视图加载时重新初始化
			Loaded += SerialDevicesListView_Loaded;
			
			// 注册Unloaded事件，在视图卸载时释放资源
			Unloaded += SerialDevicesListView_Unloaded;
		}

		private void SerialDevicesListView_Loaded(object sender, RoutedEventArgs e)
		{
			// 在视图加载时调用ViewModel的重新初始化方法
			_viewModel.Reinitialize();
		}

		private void SerialDevicesListView_Unloaded(object sender, RoutedEventArgs e)
		{
			// 视图卸载时释放ViewModel资源
			_viewModel.Dispose();
		}
	}
}